package com.yz.rabbitmq.limit;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.yz.rabbitmq.ChannelsUtil;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class LimitCon {
    public static void main(String[] args) throws Exception {
        Channel channel = ChannelsUtil.getChannel();
        channel.exchangeDeclare("fanoutExchange","fanout",true);
        channel.queueDeclare("fanout_queue",true,false,false,null);
        channel.queueBind("fanout_queue","fanoutExchange","");

        channel.basicQos(0,1,false);

        channel.basicConsume("fanout_queue",false,new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
                channel.basicAck(envelope.getDeliveryTag(),true);
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
